<?php

namespace App\Http\Controllers;

use App\Http\Model\Reason;
use App\Http\Model\User;
use App\Repositories\ArticleRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Validator;

class ArticleController extends Controller
{

    protected $article;

    public function __construct(ArticleRepository $article )
    {
        $this->article=$article;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     *
     *  /article  GET
     * 文章列表
     */
    public function index(Request $request)
    {
        $status=$request->input('category','-1');
//        dd($status);
        //过滤普通用户查看平台发布的文章
        $nowUser=User::where('name',session(config('constants.SESSION_NAME')))->value('id');

        if($nowUser!=1){

            //获取当前非超级管理员的文章
            $results=$this->article->getArticleByUser($nowUser,$status);


        }else{

            if($status!='-1'){

                //带条件搜索
                $results=$this->article->getAllByCondition('status',$status);

            }else{
                //展示除去删除的文章信息
                $results=$this->article->getArticle(['0','1','2']);

            }
        }




        //将用户名和机构名返回页面
        foreach($results as $k=>$v){

            $results[$k]['name']=User::where('id',$v['user_id'])->value('name');
            $results[$k]['organize_name']=User::where('id',$v['user_id'])->value('organize_name');

        }



        return view('admin.article.index',['results'=>$results,'status'=>$status]);
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     *
     * /article/create   GET
     */
    public function create()
    {
        $user_id=User::where('name',session(config('constants.SESSION_NAME')))->value('id');

        //
        return view('admin.create',['user_id'=>$user_id]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     *
     * /article   POST
     * 保存文章
     */
    public function store(Request $request)
    {

        $input=$request->except('_token');

        if($input){
            $validator=Validator::make($request->input(),[
                'title'=>'required',
                'content'=>'required',
                'avatar'=>'required'
            ],[
                'title.required'=>'标题不能为空',
                'content.required'=>'内容不能为空',
                'avatar.required'=>'图片不能为空'
            ]);

            if($validator->passes()){//通过验证
                $result=User::where('name',session(config('constants.SESSION_NAME')))->first();
                $input['user_id']=$result['id'];
                $input['organize_id']=$result['organize_id'];

                if($input['user_id']!=config('constants.Super_Admin')){
                    //如不是超管则进入待审核状态
                    $input['status']='0';
                }
                $this->article->store($input);//存到数据库



                return ['status_code'=>200,'msg'=>'发布成功'];
            }else{
                return ['status_code'=>500,'msg'=>$validator->errors()->first()];
            }
            return ['status_code'=>500,'msg'=>'内容输入有误'];
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     *
     * /article/{article}    GET
     * 文章展示
     */
    public function show($id)
    {
        $article=$this->article->getById($id);

        //未通过审核，返回原因及修改意见
        if($article->status==2){
            $reason=Reason::where('article_id',$article->id)->first();
        }else{
            $reason='';
        }

        return view('admin.article.detail',['article'=>$article,'reason'=>$reason]);
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     *
     * article/{article}/edit  GET
     *
     */
    public function edit($id)
    {
        $article=$this->article->getById($id);

//
//        //过滤富文本生成的标签
//        $article->content=strip_tags($article->content);

        //dd($results);
        return view('admin.article.edit',['article'=>$article]);
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     *
     * article/{article}   PUT
     * 文章更新
     *
     */
    public function update(Request $request, $id)
    {
        $input=$request->except('_token');

        if($input) {
            $validator = Validator::make($request->input(), [
                'title' => 'required',
                'content' => 'required',
                'avatar' => 'required'
            ], [
                'title.required' => '标题不能为空',
                'content.required' => '内容不能为空',
                'avatar.required' => '图片不能为空'
            ]);
            if($validator->passes()){//通过验证

                $this->article->update($id,$request->input());

                return ['status_code'=>200,'msg'=>'修改成功'];

            }else{
                return ['status_code'=>500,'msg'=>$validator->errors()->first()];
            }
        }


        return ($request);
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     *
     *article/{article}  DELETE
     * 删除文章
     */
    public function destroy($id)
    {
        if($this->article->setField('id',$id,'status','3'))
        {
            return ['msg'=>'删除成功'];
        }else{
            return ['msg'=>'删除失败,请重试'];
        }

    }

    /**
     * 文章审核
     * @param Request $request
     */
    public function apply(Request $request)
    {
       if($request->input('flag')=='1'){
           //通过审核
           if($this->article->setField('id',$request->input('article_id'),'status','1')){
               return ['status_code'=>200,'msg'=>'审核成功'];
           }else{
               return ['status_code'=>500,'msg'=>'请重试'];
           }

       }else{
           //不通过审核
          if($this->article->setField('id',$request->input('article_id'),'status','2')){

              /**
               * 创建一个新类
               */
             $reason=new Reason();
             $reason->article_id=$request->input('article_id');
              $reason->reason=$request->input('reason');
              $reason->advice=$request->input('advice');
              $reason->save();
              return ['status_code'=>200,'msg'=>'审核成功'];
          }else{
              return ['status_code'=>500,'msg'=>'请重试'];
          }

       }

    }
}
